package com.lu.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.reggie.dto.SetmealDto;
import com.lu.reggie.entity.Setmeal;
import com.lu.reggie.entity.SetmealDish;
import com.lu.reggie.exceptions.BusinessException;
import com.lu.reggie.mapper.SetmealMapper;
import com.lu.reggie.service.ICategoryService;
import com.lu.reggie.service.ISetmealDishService;
import com.lu.reggie.service.ISetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author 况山云
 * @since 2022-08-22
 */
@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {

    @Autowired
    private ISetmealDishService iSetmealDishService;
    @Autowired
    private ISetmealService iSetmealService;
    @Autowired
    private ICategoryService iCategoryService;
    /**
     * 自定义添加套餐方法
     * @param setmealDto
     */
    @Override
    @Transactional//一定要加
    public void saveWithDish(SetmealDto setmealDto) {
        //先保存setmeal表数据
        this.save(setmealDto);
        //在去setmealdish执行添加操作
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(Long.toString(setmealDto.getId()));
            return item;
        }).collect(Collectors.toList());
        System.out.println("=====================");
        log.info("菜品信息为:{}", setmealDishes);
        //保存套餐菜品信息
        iSetmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 套餐删除关联菜品数据
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //先判断套餐状态
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getStatus, 1);
        queryWrapper.in(Setmeal::getId, ids);
        int count = this.count(queryWrapper);
        if (count > 0) {
            throw new BusinessException("套餐正在销售无法删除!");
        }
        //先删除setmeal数据
        this.removeByIds(ids);
        //再去删除setmeal_dish数据，但是删除方法正能根据主键删除
        //这里的id在setmeal_dish表中不是主键，所以先需要构造过滤条件
        LambdaQueryWrapper<SetmealDish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        iSetmealDishService.remove(dishLambdaQueryWrapper);
    }

    /**
     * 修改套餐数据回显
     * @param id
     * @return
     */
    @Override
    public SetmealDto getSetmeal(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = this.getById(id);
        //需要显示该套餐已经关联的菜品,根据dishId，去setMealDish查找
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,SetmealDish::getSetmealId, id);
        //把菜品信息copy给dto
        if (setmeal != null) {
            BeanUtils.copyProperties(setmeal, setmealDto);
            List<SetmealDish> setmealDishes = iSetmealDishService.list(queryWrapper);
            setmealDto.setSetmealDishes(setmealDishes);
        }
        return setmealDto;
    }
}
